Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 27, 2025

Problem

The multi_replace_string_tool and replace_string_tool currently fail when attempting to replace a string that appears multiple times in a file, throwing an unhelpful error:

String replacement failed: Multiple matches found for the text to replace. 
Multiple exact matches found. Make your search string more specific.

This creates friction when users need to make repetitive changes across a file, forcing them to either make the search string more specific or perform multiple separate replacements.

Solution

This PR adds an optional replaceAll parameter to the IReplaceStringToolParams interface that allows replacing all occurrences of a string instead of failing when multiple matches are found.

Key Changes

Enhanced Interface

export interface IReplaceStringToolParams {
  explanation: string;
  filePath: string;
  oldString: string;
  newString: string;
  replaceAll?: boolean;  // New optional parameter
}

Improved Error Messages
When multiple matches are found and replaceAll is not set, the tool now provides actionable guidance:

Multiple exact matches found on lines 1, 2, 4. Make your search string more specific or use replaceAll: true to replace all occurrences.

Robust Implementation

  • Supports all matching strategies (exact, fuzzy, whitespace-flexible)
  • Replaces occurrences from right to left to avoid position shift issues
  • Maintains 100% backward compatibility - existing code works unchanged
  • Includes comprehensive test coverage for edge cases

Usage Example

// Before: This would fail with multiple matches error
{
  filePath: '/src/utils.ts',
  oldString: 'console.log',
  newString: 'logger.info'
}

// After: This replaces all occurrences
{
  filePath: '/src/utils.ts', 
  oldString: 'console.log',
  newString: 'logger.info',
  replaceAll: true
}

Benefits

  • Reduces friction when making repetitive changes across files
  • Provides actionable guidance with specific line numbers instead of generic error messages
  • Maintains backward compatibility - no breaking changes to existing functionality
  • Supports all matching strategies including fuzzy and whitespace-flexible matching
  • Clean implementation with comprehensive test coverage and security validation

This enhancement makes the string replacement tools significantly more user-friendly while maintaining their robustness and reliability.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/microsoft/microsoft%2Fvscode-copilot-chat/languages
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Add option to replaceAll occurances in multi_replace_string tool</issue_title>
<issue_description>Current Behavior
The multi_replace_string_tool currently fails when attempting to replace a string that appears multiple times in a file, throwing the error:

String replacement failed: Multiple matches found for the text to replace. Multiple exact matches found. Make your search string more specific.

Expected Behavior
When multiple occurrences are detected, the tool should provide clearer guidance and support the ability to replace all occurrences.


Proposed Solution
Add an optional replaceAll parameter to multi_replace_string_in_file tool

Improve error message: When multiple matches are found and replaceAll is not set, the error message should:
Suggest using replaceAll: true to replace all occurrences

Include line numbers where duplicates were found so the model can verify the edit is valid

Benefits
Reduces friction when making repetitive changes across a file
Provides actionable guidance instead of just failing</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes microsoft/vscode#268585

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor Author

Copilot AI commented Sep 27, 2025

@connor4312 Unfortunately, I hit an error while trying to use the custom Copilot setup steps configured for this repository and had to close this PR. The error I am seeing is:

No `copilot-setup-steps` job found in your `copilot-setup-steps.yml` workflow file. Please ensure you have a single job named `copilot-setup-steps`. For more details, see https://gh.io/copilot/actions-setup-steps.

Once you or someone with the necessary access fixes the problem, please unassign and then reassign issue #0 to me and I'll retry. Thanks!

@Copilot Copilot AI changed the title [WIP] Add option to replaceAll occurances in multi_replace_string tool Add replaceAll option to string replacement tools for handling multiple matches Sep 27, 2025
@Copilot Copilot AI requested a review from connor4312 September 27, 2025 17:26
Copilot finished work on behalf of connor4312 September 27, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to replaceAll occurances in multi_replace_string tool
2 participants